#include "mywidget.h"
#include "ui_mywidget.h"
#include<QFileDialog>
#include<QFileSystemModel>
#include<QTcpSocket>
#include<QMessageBox>
#include<QHostAddress>
#include<QDebug>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<QTimer>
#include<QFont>
MyWidget::MyWidget(int width,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    memset(ip,0,sizeof(char)*100);
    this->isConnect=false;
    this->pclient=NULL;
    this->flag=0;
    this->width=width;
    this->isReview=false;
    ui->setupUi(this);
    ptime=new QTimer(this);
    if(ptime==NULL)
    {
        ui->label->setText("");
        return;
    }
    this->pclient=new QTcpSocket;
    if(this->pclient==NULL)
        return;
    ui->stackedWidget->setCurrentIndex(1);
    model = new QFileSystemModel(this);
    model->setFilter(QDir::AllEntries|QDir::NoDotAndDotDot);
    root_Dir = QDir::rootPath();
    m_nCurrentPath = root_Dir.absolutePath()+"storage/emulated/0";
    model->setRootPath(m_nCurrentPath);
    ui->listView->setModel(model);
    ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
    ui->listView->show();
    ui->lineEdit->setText(m_nCurrentPath);
    connect(this->pclient,&QTcpSocket::connected,[=](){
        ui->label->setText("状态为：连接成功");
        this->isConnect=true;
     });
    connect(this->pclient,&QTcpSocket::disconnected,[=](){
        ui->label->setText("状态为：未连接");
        this->isConnect=false;
    });
    connect(this->pclient,&QTcpSocket::readyRead,[=](){
        QByteArray temp=this->pclient->readAll();
        this->dealBackIt(temp.data(),temp.size());
    });
    connect(this->ptime,&QTimer::timeout,[=](){
        ui->label->setText("状态为：服务器接收成功");
        if(ptime->isActive()==true)
            ptime->stop();
    });
    FILE* fp=fopen("./ip.txt","r+");
    if(fp==NULL)
    {
        fp=fopen("./ip.txt","w+");
        if(fp!=NULL)
        {
            fprintf(fp,"127.0.0.1");
            strcpy(ip,"127.0.0.1");
            fclose(fp);
        }
    }
    else
    {
        if(fscanf(fp,"%s",ip)==0)
        {
            memset(ip,0,sizeof(char)*100);
            strcpy(ip,"127.0.0.1");
            fclose(fp);
        }
        else
            fclose(fp);
    }
    ui->label->setText(QString("状态为：%1未连接").arg(ip));
    ui->label_8->setText(QString("目前：%1").arg(ip));
    this->pclient->connectToHost(QHostAddress(QString(ip)),5209);//125.73.58.176
}

MyWidget::~MyWidget()
{
    delete ui;
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:change the index
*/
void MyWidget::on_pushButton_2_clicked()
{
    if(isConnect==true)
        ui->label_6->setText("状态为：连接成功");
    ui->stackedWidget->setCurrentIndex(2);
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:come into the load
*/
void MyWidget::on_pushButton_6_clicked()
{
    index = ui->listView->currentIndex();
    if(m_nCurrentPath == "/")
        m_nCurrentPath = "";
    m_nCurrentPath = m_nCurrentPath + "/"+ index.data().toString();
    ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
    ui->listView->show();
    ui->lineEdit->setText(m_nCurrentPath);
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:user to change the root to update
*/
void MyWidget::on_pushButton_4_clicked()
{
    m_nCurrentPath=ui->lineEdit->text();
    ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
    ui->listView->show();
    ui->lineEdit->setText(m_nCurrentPath);
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:back to last load
*/
void MyWidget::on_pushButton_7_clicked()
{
    if(m_nCurrentPath.length()!= 0)
    {
        if(m_nCurrentPath=="/")
        {
            m_nCurrentPath = root_Dir.absolutePath()+"storage/emulated/0/";
            ui->stackedWidget->setCurrentIndex(1);
            model->setRootPath(m_nCurrentPath);
            ui->listView->setModel(model);
            ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
            ui->listView->show();
            ui->lineEdit->setText(m_nCurrentPath);
        }
        current_Dir = QDir(m_nCurrentPath);
        current_Dir.cdUp();
        m_nCurrentPath = current_Dir.absolutePath();
        ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
        ui->listView->show();
        ui->lineEdit->setText(m_nCurrentPath);
    }
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:down load file
*/
void MyWidget::on_pushButton_5_clicked()
{
    int i=0;
    float rata=0;
    if(isConnect==false)
    {
        ui->label_6->setText("状态为：未连接");
        return;
    }
    ui->label_6->setText("状态为：加载中");
    index = ui->listView->currentIndex();
    if(m_nCurrentPath == "/")
        m_nCurrentPath = "";
    QString tempForPath=m_nCurrentPath;
    m_nCurrentPath = m_nCurrentPath + "/"+ index.data().toString();
    m_ChoosePath = m_nCurrentPath;
    ui->lineEdit->setText(m_ChoosePath);
    if(ui->lineEdit->text()[0]!='/')
        m_ChoosePath=ui->lineEdit->text();
    std::string stdPath=m_ChoosePath.toStdString();
    char* fileName=strrchr(stdPath.c_str(),'/');
    if(fileName==NULL)
    {
        ui->label_6->setText("状态为:文件路径有误");
        return;
    }
    strcpy(file.name,fileName+1);
    ui->lineEdit->setText(file.name);
    if(strcmp(file.name,"Disk")==0||strcmp(file.name,"Disk.cpp")==0)
    {
        ui->label_6->setText("状态为：禁止上传该文件");
        ui->lineEdit->setText(tempForPath);
        m_nCurrentPath=tempForPath;
        model->setRootPath(m_nCurrentPath);
        ui->listView->setModel(model);
        ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
        ui->listView->show();
        return;
    }
    FILE* fp=fopen(stdPath.c_str(),"rb+");
    if(fp==NULL)
    {
        ui->label_6->setText("状态为：文件名有误");
        ui->lineEdit->setText(stdPath.c_str());
        return;
    }
    fseek(fp,0,SEEK_END);
    file.len=ftell(fp);
    if(file.len==0)
        return;
    fseek(fp,0,SEEK_SET);
    file.flag=1;
    int temp=this->pclient->write((char*)&this->file,sizeof(Msg));
    if(temp<=0)
    {
        ui->label->setText("状态为：第一次发送失败");
        return;
    }
    ui->label->setText("状态为：第一次发送成功");
    char* sen=(char*)malloc(sizeof(char)*(file.len+1));
    memset(sen,0,sizeof(char)*(file.len+1));
    if(sen==NULL)
    {
        ui->label->setText("状态为：内存分配失败");
        return;
    }
    for(i=0;i<file.len;i++)
    {
        sen[i]=fgetc(fp);
        if(sen[i]==0)
            qDebug()<<i<<endl;
    }
    sen[i+1]=0;
    fclose(fp);
    temp=this->pclient->write(sen,sizeof(char)*(file.len));
    qDebug()<<temp<<"  i="<<i<<endl;
    if(temp<=0)
    {
        ui->label->setText("状态为：第二次发送失败");
        return;
    }
    ui->label->setText("状态为：等待服务器接收");
    ui->stackedWidget->setCurrentIndex(1);
    free(sen);
    rata=file.len/112600;
    ui->label_3->setText(QString("长度为：%1发送 等待%2s").arg(file.len).arg(rata));
    QString tempStr=dealLabelText(ui->label_4->font(),QString("名字为：%1").arg(file.name),this->width-50);
    ui->label_4->setText(tempStr);
    if(ptime->isActive()==false&&rata>1)
        ptime->start(rata*1000);
    if(ptime->isActive()==false)
        ui->label->setText("状态为：传输成功");
    ui->lineEdit->setText(tempForPath);
    m_nCurrentPath=tempForPath;
    model->setRootPath(m_nCurrentPath);
    ui->listView->setModel(model);
    ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
    ui->listView->show();
    ui->label_6->setText("状态为：发送成功");
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:way to deal back mess=age
*/
void MyWidget::dealBackIt(char* data,int len)
{
    static int all=0,continueGet=0;
    if(flag%2==0)
    {
        flag++;
        file.len=((Msg*)data)->len;
        strcpy(file.name,((Msg*)data)->name);
        file.flag=((Msg*)data)->flag;
        ui->label->setText("状态为：获取信息成功");
        if(file.flag!=0&&file.flag!=1&&file.flag!=2&&file.flag!=3&&file.flag!=4&&file.flag!=5&&file.flag!=6&&file.flag!=7)
        {
            flag--;
            return;
        }
        if(file.len<=0)
            flag--;
        if(file.flag==1||file.flag==2)
        {
            ui->label_4->setText(dealLabelText(ui->label_4->font(),QString("名字为：%1").arg(file.name),this->width-50));
            ui->label_3->setText(QString("长度为：%1").arg(file.len));
        }
        if(file.flag==3)
        {
            ui->label->setText("状态为：删除成功");
            flag--;
        }
        else if(file.flag==4)
        {
            ui->label->setText("状态为：操作失败");
            ui->label_17->setText("文件：获取失败");
            flag--;
        }
        else if(file.flag==5)
        {
            ui->label->setText("状态为：改名成功");
            flag--;
        }
        else if(file.flag==6)
        {
            ui->label_12->setText(QString("占用为：%1M").arg(file.len));
            flag--;
        }
        else if(file.flag==7)
        {
            ui->label->setText("状态为：服务器接收成功");
            ui->label_3->setText(QString("长度为：%1发送结束").arg(file.len));
            flag--;
        }
    }
    else if(file.flag==2&&isReview==false)
    {
        if(file.len-len<=5&&continueGet==0)
        {
            int i=0;
            flag++;
            ui->label->setText("状态为：第二次成功");
            char name[100]="/storage/emulated/0/";
            strcat(name,file.name);
            FILE* fp=fopen(name,"wb+");
            if(fp==NULL)
            {
                ui->label->setText("状态为：创建失败");
                return;
            }
            for(i=0;i<len;i++)
                fputc(data[i],fp);
            ui->stackedWidget->setCurrentIndex(1);
            ui->label->setText("状态为：传输成功");
            file.len=0;
            memset(file.name,0,100);
            fclose(fp);
            ui->label_4->setText(dealLabelText(ui->label_4->font(),QString("名字为：%1").arg(file.name),this->width-50));
            ui->label_3->setText(QString("长度为：%1").arg(file.len));
        }
        else
        {
            char name[100]="/storage/emulated/0/";
            strcat(name,file.name);
            all+=len;
            FILE* fp=fopen(name,"ab");
            if(fp==NULL)
                fp=fopen(name,"wb+");
            if(fp==NULL)
            {
                ui->label->setText("状态为：无权限");
                return;
            }
            for(int i=0;i<len;i++)
                fputc(data[i],fp);
            fclose(fp);
            ui->label_3->setText(QString("长度为：%1 收到%2").arg(file.len).arg(all));
            continueGet=1;
            if(file.len-all<=5)
            {
                flag++;
                all=0;
                continueGet=0;
                ui->stackedWidget->setCurrentIndex(1);
                ui->label->setText("状态为：传输成功");
                ui->label_3->setText(QString("状态为：%1 接收").arg(file.len));
                file.len=0;
                memset(file.name,0,100);
                fclose(fp);
                ui->label_3->setText(QString("长度为：%1 收到%2").arg(file.len).arg(all));
                ui->label_4->setText(QString("名字为：%1").arg(file.name));
            }
        }
    }
    else if(file.flag==2&&isReview==true)
    {
        if(file.len-len<=5&&continueGet==0)
        {

            flag++;
            ui->textBrowser_4->setText(data);
            file.len=0;
            memset(file.name,0,100);
            isReview=false;
        }
        else
        {
            all+=len;
            continueGet=1;
            if(file.len<100000)
                ui->textBrowser_4->append(data);
            else
                ui->textBrowser_4->setText("文件过大无法显示");
            if(file.len-all<=5)
            {
                flag++;
                all=0;
                continueGet=0;
                file.len=0;
                memset(file.name,0,100);
                isReview=false;
            }
        }
    }
    else if(file.flag==0)
    {
        static int all=0,isFirst=1;
        if(file.len-len<5)
        {
            ui->textBrowser->setText(data);
            flag++;
        }
        else
        {
            if(isFirst)
            {
                ui->textBrowser->setText(data);
                isFirst=0;
            }
            else
                ui->textBrowser->append(data);
            all+=len;
            if(file.len-all<5)
            {
                flag++;
                all=0;
                isFirst=1;
            }
        }
    }
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:file the rometo message
*/
void MyWidget::on_pushButton_3_clicked()
{
    int temp=0;
    if(isConnect==false)
        return;
    ui->stackedWidget->setCurrentIndex(3);
    file.flag=0;
    temp=pclient->write((char*)&file,sizeof(Msg));
    if(temp<=0)
        ui->label->setText("状态为：发送数据失败");
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:send message down load
*/
void MyWidget::on_pushButton_8_clicked()
{
    int temp=0;
    QString name=ui->lineEdit_3->text();
    std::string stdName=name.toStdString();
    if(name==NULL)
        return;
    strcpy(file.name,stdName.c_str());
    file.flag=2;
    temp=pclient->write((char*)&file,sizeof(Msg));
    if(temp<=0)
        ui->label->setText("状态为：发送数据失败");
    ui->stackedWidget->setCurrentIndex(1);
    ui->label_4->setText(dealLabelText(ui->label_4->font(),QString("名字为：%1").arg(file.name),this->width-50));
    ui->lineEdit_3->clear();
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:change the stack
*/
void MyWidget::on_pushButton_9_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:compare to celepate
*/
void MyWidget::on_pushButton_10_clicked()
{
    QString line3=ui->lineEdit_3->text();
    QString data=ui->textBrowser->toPlainText();
    std::string stddata=data.toStdString();
    std::string stdlin=line3.toStdString();
    char* ptemp=strstr(stddata.c_str(),stdlin.c_str());
    if(ptemp==NULL)
        return;
    char name[100]={0};
    sscanf(ptemp,"%s",name);
    ui->lineEdit_3->setText(name);
}

void MyWidget::on_pushButton_11_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MyWidget::on_pushButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:change ip
*/
void MyWidget::on_pushButton_12_clicked()
{
    QString changIp=ui->lineEdit_2->text();
    std::string stdIp=changIp.toStdString();
    if(changIp==NULL)
        return;
    memset(ip,0,sizeof(char)*100);
    strcpy(ip,stdIp.c_str());
    FILE* fp=fopen("./ip.txt","r+");
    if(fp==NULL)
    {
        ui->label_8->setText("目前：更改失败");
        return;
    }
    fclose(fp);
    fp=fopen("./ip.txt","w+");
    if(fp==NULL)
    {
        ui->label_8->setText("目前：更改失败");
        return;
    }
    fprintf(fp,"%s",ip);
    fclose(fp);
    ui->label_8->setText("目前：更改成功");
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:delete the file
*/
void MyWidget::on_pushButton_13_clicked()
{
    int temp=0;
    QString name=ui->lineEdit_3->text();
    std::string stdName=name.toStdString();
    if(name==NULL)
        return;
    if(strcmp(stdName.c_str(),"Disk")==0||strcmp(stdName.c_str(),"Disk.cpp")==0)
        return;
    strcpy(file.name,stdName.c_str());
    file.flag=3;
    temp=pclient->write((char*)&file,sizeof(Msg));
    if(temp<=0)
    {
        ui->label->setText("状态为：");
        return;
    }
    ui->stackedWidget->setCurrentIndex(1);
    ui->label->setText(dealLabelText(ui->label->font(),QString("状态为：删除%1").arg(file.name),this->width-50));
    ui->lineEdit_3->clear();
    ui->label_3->setText("长度为：");
    ui->label_4->setText("名字为：");
}
/*
    author:chenxuan
    day:2021-08-08
    funtion:copy the file
*/
void MyWidget::on_pushButton_17_clicked()
{
    ui->lineEdit_4->setText(ui->lineEdit_3->text());
    ui->stackedWidget->setCurrentIndex(4);
    ui->textBrowser_2->setText(ui->textBrowser->toPlainText());
}

void MyWidget::on_pushButton_16_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}
/*
    author:chenxuan
    day:2021-08-09
    funtion:the same to last
*/
void MyWidget::on_pushButton_14_clicked()
{
    QString line4=ui->lineEdit_4->text();
    QString data=ui->textBrowser_2->toPlainText();
    std::string stddata=data.toStdString();
    std::string stdlin=line4.toStdString();
    char* ptemp=strstr(stddata.c_str(),stdlin.c_str());
    if(ptemp==NULL)
        return;
    char name[100]={0};
    sscanf(ptemp,"%s",name);
    ui->lineEdit_4->setText(name);
}
/*
    author:chenxuan
    day:2021-08-09
    funtion:rename the file
*/
void MyWidget::on_pushButton_15_clicked()
{
    int temp=0;
    QString line4=ui->lineEdit_4->text();
    QString line5=ui->lineEdit_5->text();
    if(line4==NULL||line5==NULL)
        return;
    if(line4=="Disk"||line4=="Disk.cpp")
        return;
    file.flag=5;
    ui->stackedWidget->setCurrentIndex(1);
    std::string std4=line4.toStdString();
    std::string std5=line5.toStdString();
    sprintf(file.name,"%s\t%s",std4.c_str(),std5.c_str());
    temp=pclient->write((char*)&file,sizeof(Msg));
    if(temp<=0)
    {
        ui->label->setText("状态为：");
        return;
    }
    ui->label->setText(QString("状态为：重命名中"));
    ui->lineEdit_4->clear();
    ui->lineEdit_5->clear();
}

void MyWidget::on_pushButton_18_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}
/*
    author:chenxuan
    day:2021-08-17
    funtion:send to ask for memory help
*/
void MyWidget::on_pushButton_19_clicked()
{
    int temp=0;
    if(isConnect==false)
        return;
    ui->stackedWidget->setCurrentIndex(5);
    file.flag=6;
    temp=pclient->write((char*)&file,sizeof(Msg));
    if(temp<0)
    {
        ui->label_12->setText("发送失败");
        return;
    }
}

void MyWidget::on_pushButton_20_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}
/*
    author:chenxuan
    day:2021-08-21
    funtion:update the get list
*/
void MyWidget::on_pushButton_21_clicked()
{
    int temp=0;
    if(isConnect==false)
        return;
    ui->stackedWidget->setCurrentIndex(3);
    file.flag=0;
    temp=pclient->write((char*)&file,sizeof(Msg));
    if(temp<=0)
        ui->label->setText("状态为：发送数据失败");
    ui->label_3->setText("长度为：");
    ui->label_4->setText("名字为：");
    ui->lineEdit_3->clear();
    ui->lineEdit_5->clear();
}
/*
    author:chenxuan
    day:2021-08-21
    funtion:review the file
*/
void MyWidget::on_pushButton_22_clicked()
{
    int temp=0;
    this->isReview=true;
    ui->textBrowser_4->clear();
    QString name=ui->lineEdit_3->text();
    std::string stdName=name.toStdString();
    if(name==NULL)
        return;
    ui->stackedWidget->setCurrentIndex(6);
    strcpy(file.name,stdName.c_str());
    file.flag=2;
    temp=pclient->write((char*)&file,sizeof(Msg));
    ui->label_17->setText(dealLabelText(ui->label_17->font(),QString("文件：%1").arg(file.name),this->width-50));
    if(temp<=0)
        ui->textBrowser_4->setText("获取失败");
}

void MyWidget::on_pushButton_23_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}
/*
    author:chenxuan
    day:2021-08-22
    funtion:deal too long label string
*/
QString MyWidget::dealLabelText(QFont font,QString str,int maxWidth)
{
    if(str==NULL)
        return "";
    QFontMetrics fontClass(font);
    int width=fontClass.width(str);
    if(width>=maxWidth)
        str=fontClass.elidedText(str,Qt::ElideRight,maxWidth);
    return str;
}
/*
    author:chenxuan
    day:2021-08-22
    funtion:jump to qq file
*/
void MyWidget::on_pushButton_24_clicked()
{
    QString qqFile=ui->lineEdit_6->text();
    if(qqFile==NULL)
        return;
    ui->stackedWidget->setCurrentIndex(2);
    m_nCurrentPath=qqFile;
    ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
    ui->listView->show();
    ui->lineEdit->setText(m_nCurrentPath);
}
/*
    author:chenxuan
    day:2021-08-22
    funtion:weixing file
*/
void MyWidget::on_pushButton_25_clicked()
{
    QString qqFile=ui->lineEdit_7->text();
    if(qqFile==NULL)
        return;
    ui->stackedWidget->setCurrentIndex(2);
    m_nCurrentPath=qqFile;
    ui->listView->setRootIndex(model->setRootPath(m_nCurrentPath));
    ui->listView->show();
    ui->lineEdit->setText(m_nCurrentPath);
}
